<template>
  <div class="mod-pro">
    <div class="container" v-if="isLogin">
      <div class="mod-pro-left">
        <ul class="mod-pro-left-ul clearfix" v-if="left >= 4">
          <li
            :class="[
              'mod-pro-left-item',
              { 'mod-pro-left-item-current': currentItem == 0 },
            ]"
            @click="clickItem(0)"
          >
            <p>我的歌手（{{ artists }}）</p>
          </li>
          <li
            :class="[
              'mod-pro-left-item',
              { 'mod-pro-left-item-current': currentItem == 1 },
            ]"
            @click="clickItem(1)"
          >
            <p>我的关注（{{ follows }}）</p>
          </li>
          <li
            :class="[
              'mod-pro-left-item',
              { 'mod-pro-left-item-current': currentItem == 2 },
            ]"
            @click="clickItem(2)"
          >
            <p>我的粉丝（{{ fans }}）</p>
          </li>
          <li
            :class="[
              'mod-pro-left-item',
              { 'mod-pro-left-item-current': currentItem == 3 },
            ]"
            @click="clickItem(3)"
          >
            <p>我的视频（{{ videos }}）</p>
          </li>
          <li class="mod-pro-left-playlist">
            <span class="pro-item-icon" @click="openCreate = !openCreate">
              <i class="iconfont" v-if="!openCreate">&#xec4a;</i>
              <i class="iconfont" v-if="openCreate">&#xec4b;</i>
            </span>
            <p>创建的歌单（{{ createLists.length }}）</p>
            <span class="addPlaylist" @click="clickAddPlaylist">
              <i class="iconfont">&#xe603;</i>
              新建
            </span>
            <ul class="pro-playlist-ul clearfix" v-if="openCreate">
              <li
                @click="showPlaylist(item.id, 4 + index)"
                :class="[
                  'pro-playlist-li',
                  { 'mod-pro-left-item-current': currentItem == 4 + index },
                ]"
                v-for="(item, index) in createLists"
                :key="item.id"
              >
                <div class="pro-playlist-li-item">
                  <div class="pro-playlist-li-left">
                    <a href="javascript:;">
                      <img :src="item.coverImgUrl" :alt="item.name" />
                    </a>
                  </div>
                  <p class="pro-playlist-li-item-name">
                    <a href="javascript:;" v-if="index == 0">我喜欢的音乐</a>
                    <a href="javascript:;" v-else>{{ item.name }}</a>
                  </p>
                  <p class="pro-playlist-li-item-count">
                    {{ item.trackCount }}首
                  </p>
                  <span v-if="item.specialType != 5">
                    <span
                      class="pro-playlist-li-item-edit"
                      @click.stop="editPlaylist(item.id)"
                    >
                      <i class="iconfont">&#xec53;</i>
                    </span>
                    <span
                      class="pro-playlist-li-item-delete"
                      @click.stop="deletePlaylist(item.id, item.name)"
                    >
                      <i class="iconfont">&#xe617;</i>
                    </span>
                  </span>
                </div>
              </li>
            </ul>
          </li>
          <li class="mod-pro-left-playlist">
            <span class="pro-item-icon" @click="openAdd = !openAdd">
              <i class="iconfont" v-if="!openAdd">&#xec4a;</i>
              <i class="iconfont" v-if="openAdd">&#xec4b;</i>
            </span>
            <p>收藏的歌单（{{ likedLists.length }}）</p>
            <ul class="pro-playlist-ul clearfix" v-if="openAdd">
              <li
                @click="showPlaylist(item.id, 4 + createLists.length + index)"
                :class="[
                  'pro-playlist-li',
                  {
                    'mod-pro-left-item-current':
                      currentItem == 4 + createLists.length + index,
                  },
                ]"
                v-for="(item, index) in likedLists"
                :key="item.id"
              >
                <div class="pro-playlist-li-item">
                  <div class="pro-playlist-li-left">
                    <a href="javascript:;">
                      <img :src="item.coverImgUrl" :alt="item.name" />
                    </a>
                  </div>
                  <p class="pro-playlist-li-item-name">
                    <a href="javascript:;">{{ item.name }}</a>
                  </p>
                  <p class="pro-playlist-li-item-count">
                    {{ item.trackCount }}首 by {{ item.creator.nickname }}
                  </p>
                  <span
                    class="pro-playlist-li-item-delete"
                    @click.stop="deletePlaylist(item.id, item.name)"
                  >
                    <i class="iconfont">&#xe617;</i>
                  </span>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <div class="mod-pro-right" ref="modProRight">
        <!-- v-if="isRouterAlive" -->
        <keep-alive>
          <router-view
            v-if="isRouterAlive"
            :aTotal="artists"
            :foTotal="follows"
            :faTotal="fans"
            :vTotal="videos"
            @pScroll="pScroll"
            :key="$route.fullPath"
            @updateP="getPlaylists(true)"
          ></router-view>
        </keep-alive>
      </div>
      <div class="delTip" id="delTip" v-if="showDelTip">
        <div class="my-del-tip" id="mdt">
          <h4 class="my-del-hd">
            提示
            <span class="close" @click="showDelTip = false">
              <i class="iconfont">&#xec47;</i>
              关闭
            </span>
          </h4>
          <div class="my-del-bd">
            <div class="my-del-cont">
              <p>
                确定删除歌单：<span class="my-del-playlist">{{
                  delPlaylistName
                }}</span>
                ？
              </p>
            </div>
            <div class="my-del-btn">
              <el-button type="primary" size="small" @click="sureDel"
                >确定</el-button
              >
              <el-button size="small" @click="showDelTip = false"
                >取消</el-button
              >
            </div>
          </div>
        </div>
      </div>
      <small-tip :tipMessage="smallTipMsg" v-if="showSmallTip"></small-tip>
    </div>
    <div class="not-logged" v-if="!isLogin">
      <span class="not-gif">
        <img :src="notLogged" alt="乾心寿" />
      </span>
      <span class="not-content">登录后才可查看</span>
    </div>
    <my-add
      @updateP="getPlaylists(true)"
      v-if="showAdd"
      :isMy="true"
      @closeAdd="showAdd = false"
    ></my-add>
  </div>
</template>

<script>
import MyAdd from "views/my/child/MyAdd";
import notLogged from "assets/images/notLogged.gif";
import SmallTip from "components/tip/SmallTip";

export default {
  components: {
    MyAdd,
    SmallTip,
  },
  provide() {
    return {
      reload: this.reload,
    };
  },
  computed: {
    fullPath: function () {
      if (this.$route.path == "/my/playlist") {
        return this.$route.fullPath;
      } else {
        return this.$route.path;
      }
    },
  },
  watch: {
    // "$store.state.sid": function () {
    //   console.log(this.$store.state.sid);
    //   this.getPlaylists(true);
    // },
  },
  data() {
    return {
      notLogged: notLogged,
      isLogin: true,
      left: 0,
      // 左边currentItem
      currentItem: 4,
      // 打开创建的歌单
      openCreate: true,
      openAdd: true,
      createLists: [],
      likedLists: [],
      // 用来标记创建的歌单数
      flag: 0,
      // 我的id
      id: 0,
      // 刷新子组件
      isRouterAlive: true,
      artists: 0,
      follows: 0,
      fans: 0,
      videos: 0,
      showAdd: false,
      // 删除操作提示
      showDelTip: false,
      dpId: 0,
      delPlaylistName: "",
      showSmallTip: false,
      smallTipMsg: "操作提示",
    };
  },
  mounted() {
    // console.log(this.$store.state.sid);
    this.getLoginStatus();
    if (this.$route.path == "/my/artists") {
      this.currentItem = 0;
      this.playMyArtists();
    } else if (this.$route.path == "/my/follows") {
      this.currentItem = 1;
      this.playMyFollows();
    } else if (this.$route.path == "/my/fans") {
      this.currentItem = 2;
      this.playMyFans();
    } else if (this.$route.path == "/my/videos") {
      this.currentItem = 3;
      this.playMyVideos();
    }
    // console.log(this.$store.state.isLogin);
    // if (this.$store.state.isLogin) {
    //   console.log("mymy");
    //   this.myLoad = true;
    //   console.log(this.$store.state.profile);
    //   this.id = this.$store.state.profile.userId;
    //   this.getPlaylists();
    //   this.getArtistsCount();
    //   this.getVideosCount();
    //   this.getFollowsCount();
    //   this.isLogin = true;
    // } else {
    //   this.isLogin = false;
    // }
  },
  watch: {
    "$store.state.isLogin": function () {
      // console.log(this.$store.state.isLogin);
      if (this.$store.state.isLogin) {
        this.isLogin = true;
        this.getLoginStatus();
      }
    },
  },
  methods: {
    editPlaylist(id) {
      console.log("edit", id);
    },
    sureDel() {
      this.axios({
        url: "/playlist/delete?id=" + this.dpId + "&timerstame=" + Date.now(),
      })
        .then((res) => {
          this.smallTipMsg = "删除歌单成功！";
          this.showSmallTip = true;
          this.showDelTip = false;
          setTimeout(() => {
            this.showSmallTip = false;
          }, 1000);
          this.getPlaylists(true);
        })
        .catch((err) => {
          console.log(err);
          this.smallTipMsg = "删除歌单失败！";
          this.showSmallTip = true;
          this.showDelTip = false;
          setTimeout(() => {
            this.showSmallTip = false;
          }, 1000);
        });
    },
    deletePlaylist(id, name) {
      this.showDelTip = true;
      this.dpId = id;
      this.delPlaylistName = name;
    },
    clickAddPlaylist() {
      this.showAdd = true;
      // console.log("新建歌单");
    },
    pScroll(h) {
      console.log(h);
      this.$refs.modProRight.scrollTo({
        top: h,
        behavior: "smooth",
      });
    },
    getFollowsCount() {
      this.axios({
        url: "/user/detail?uid=" + this.id,
      })
        .then((res) => {
          this.left++;
          // console.log(this.follows);
          // console.log(res.data.profile.follows);
          this.follows = res.data.profile.follows;
          this.fans = res.data.profile.followeds;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getArtistsCount() {
      this.axios({
        url: "/artist/sublist",
      })
        .then((res) => {
          this.left++;
          this.artists = res.data.count;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getVideosCount() {
      this.axios({
        url: "/mv/sublist",
      })
        .then((res) => {
          this.left++;
          this.videos = res.data.count;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getLoginStatus() {
      console.log("getLoginStatus");
      // 获取登录状态
      this.axios({
        url: "/login/status?timerstamp=" + Date.now(),
      })
        .then((res) => {
          // console.log(res.data);
          if (res.data.data.profile) {
            // 已登录
            // console.log("已登录");
            // this.$store.state.isLogin = true;
            this.$store.commit("changeIsLogin", true);
            this.isLogin = true;
            this.id = this.$store.state.profile.userId;
            this.left = 0;
            this.getPlaylists();
            this.getArtistsCount();
            this.getVideosCount();
            this.getFollowsCount();
          } else {
            // 未登录
            // console.log("未登录");
            this.$store.state.isLogin = false;
            this.isLogin = false;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    reload() {
      // 重加载 没用上
      this.isRouterAlive = false;
      this.$nextTick(() => {
        console.log("nextTick");
        this.isRouterAlive = true;
      });
    },
    showPlaylist(id, index) {
      console.log(id);
      this.currentItem = index;
      this.playMyPlaylist(id);
    },
    clickItem(index) {
      this.currentItem = index;
      switch (index) {
        case 0:
          this.playMyArtists();
          break;
        case 1:
          this.playMyFollows();
          break;
        case 2:
          this.playMyFans();
          break;
        case 3:
          this.playMyVideos();
          break;
      }
    },
    getPlaylists(update = false) {
      this.axios({
        url:
          "/user/playlist?limit=1000&uid=" +
          this.id +
          "&timerstamp=" +
          Date.now(),
      })
        .then((res) => {
          // console.log(res);
          this.left++;
          this.flag = 0;
          for (let item of res.data.playlist) {
            if (item.creator.userId == this.id) {
              this.flag++;
            } else {
              break;
            }
          }
          this.createLists = res.data.playlist.slice(0, this.flag);
          this.likedLists = res.data.playlist.slice(this.flag);
          if (!update) {
            if (
              this.$route.path == "/my/playlist" ||
              this.$route.path == "/my"
            ) {
              this.playMyPlaylist(res.data.playlist[0].id);
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
.mod-pro {
  position: relative;
  width: 100vw;
  height: calc(100vh - 90px);
  background-color: var(--color-theme-light-5);
}
.mod-pro .container {
  background-color: var(--color-theme-light-4);
}
.mod-pro-left {
  position: relative;
  float: left;
  padding: 50px 0 60px 0;
  width: 350px;
  height: calc(100vh - 90px);
  overflow-y: auto;
  border-right: 1px solid rgba(0, 0, 0, 0.2);
}
.mod-pro-right {
  position: relative;
  float: left;
  padding: 50px 10px 60px 0;
  width: 850px;
  height: calc(100vh - 90px);
  overflow-x: hidden;
  overflow-y: auto;
}
.mod-pro-left::-webkit-scrollbar,
.mod-pro-right::-webkit-scrollbar {
  display: block;
  width: 5px;
}
.mod-pro-left::-webkit-scrollbar-track,
.mod-pro-right::-webkit-scrollbar-track {
  background-color: rgba(0, 0, 0, 0.2);
}
.mod-pro-left::-webkit-scrollbar-thumb,
.mod-pro-right::-webkit-scrollbar-thumb {
  background-color: rgba(0, 0, 0, 0.5);
}

.mod-pro-left .mod-pro-left-item,
.mod-pro-left .mod-pro-left-playlist {
  position: relative;
  padding-left: 50px;
  min-height: 38px;
  line-height: 38px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.mod-pro-left .mod-pro-left-item:hover,
.mod-pro-left .pro-playlist-li:hover,
.mod-pro-left .mod-pro-left-item-current {
  background-color: rgba(0, 0, 0, 0.1);
}
.mod-pro-left .mod-pro-left-playlist .pro-item-icon {
  position: absolute;
  left: 30px;
}
.mod-pro-left-playlist .addPlaylist {
  display: inline-block;
  position: absolute;
  top: 7px;
  right: 10px;
  padding: 3px 8px;
  line-height: 14px;
  font-size: 12px;
  border: 1px solid #ccc;
  background-color: #fff;
  border-radius: 3px;
}
.mod-pro-left-playlist .addPlaylist:hover {
  text-decoration: underline;
}
.mod-pro-left-playlist .addPlaylist .iconfont {
  font-size: 14px;
  color: var(--color-theme);
}
/* pro-playlist-ul */
.pro-playlist-li {
  position: relative;
  padding: 10px 20px;
}
.pro-playlist-li-item {
  position: relative;
  margin-left: 50px;
  height: 40px;
}
.pro-playlist-li-item .pro-playlist-li-left {
  position: absolute;
  margin-left: -50px;
  overflow: hidden;
  width: 40px;
  height: 40px;
}
.pro-playlist-li-item .pro-playlist-li-left a {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 40px;
}
.pro-playlist-li-item .pro-playlist-li-left a img {
  width: 40px;
  height: 40px;
}
.pro-playlist-li-item-name {
  height: 20px;
  line-height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}
.pro-playlist-li-item-name a {
  white-space: nowrap;
}
.pro-playlist-li-item-count {
  height: 20px;
  line-height: 20px;
  font-size: 12px;
  color: #999;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pro-playlist-li-item-edit {
  display: none;
  position: absolute;
  bottom: 2px;
  right: 26px;
  height: 14px;
  line-height: 14px;
}
.pro-playlist-li-item-delete {
  display: none;
  position: absolute;
  bottom: 2px;
  right: 5px;
  height: 14px;
  line-height: 14px;
}
.pro-playlist-li-item-edit .iconfont,
.pro-playlist-li-item-delete .iconfont {
  font-size: 14px;
  color: #999;
  cursor: pointer;
}
.pro-playlist-li-item-edit .iconfont:hover,
.pro-playlist-li-item-delete .iconfont:hover {
  color: var(--color-theme);
}
.pro-playlist-li:hover .pro-playlist-li-item-edit,
.pro-playlist-li:hover .pro-playlist-li-item-delete {
  display: inline-block;
}
/* not-logged */
.mod-pro .not-logged {
  position: relative;
  width: 1200px;
  height: calc(100vh - 90px);
  margin: 0 auto;
  overflow: hidden;
}
.mod-pro .not-logged span {
  position: absolute;
  top: 200px;
  left: 50%;
  transform: translateX(-50%);
}
.mod-pro .not-logged .not-content {
  top: 520px;
  text-align: center;
}
/* my-del-tip */
.delTip {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1006;
}
.my-del-tip {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  height: 200px;
  width: 500px;
  background-color: var(--color-theme-light-5);
  border-radius: 10px;
  box-shadow: 0 0 10px 3px #999;
  overflow: hidden;
}
.my-del-hd {
  padding: 0 10px 0 20px;
  height: 40px;
  line-height: 40px;
  font-size: 20px;
  color: #f2f2f2;
  background-color: var(--color-theme);
  cursor: move;
}
.my-del-hd .close {
  position: absolute;
  right: 5px;
  width: 30px;
  height: 30px;
  text-align: center;
  font-size: 0;
  cursor: pointer;
}
.my-del-hd .close .iconfont {
  font-size: 16px;
}
.my-del-bd {
  position: relative;
  width: 100%;
  height: 160px;
  text-align: center;
}
.my-del-bd .my-del-cont {
  position: relative;
  height: 110px;
  line-height: 30px;
  font-size: 16px;
  color: #000;
}
.my-del-bd .my-del-cont p {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 480px;
  overflow: hidden;
  white-space: normal;
  overflow-wrap: break-word;
}
.my-del-bd .my-del-playlist {
  color: var(--color-theme);
}
.my-del-bd .my-del-btn {
  display: flex;
  justify-content: space-evenly;
}
</style>